package com.zrt.bookstore3.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrt.bookstore3.Dao.BookDao;
import com.zrt.bookstore3.Dao.UserDao;
import com.zrt.bookstore3.Domain.Book;
import com.zrt.bookstore3.Domain.User;
import com.zrt.bookstore3.Service.BookService;
import com.zrt.bookstore3.Service.UserService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<BookDao,Book> implements BookService {

    @Autowired
    private BookDao bookDao;

    @Override
    public boolean delete(Integer id) {
        return bookDao.deleteById(id) > 0;
    }

    @Override
    public List<Book> listBooksByType(String type) {
        return null;
    }

    @Override
    public List<Book> selectAll() {
        return bookDao.selectAll();
    }

    @Override
    public IPage<Book> getPage(int currentPage, int pageSize, Book book) {
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<Book>();
        lqw.like(Strings.isNotEmpty(book.getName()),Book::getName,book.getName());
        lqw.like(Strings.isNotEmpty(book.getType()),Book::getType,book.getType());

        IPage page = new Page(currentPage,pageSize);
        bookDao.selectPage(page,lqw);
        return page;
    }

    @Override
    public Book selectById(int id) {
        return bookDao.selectById(id);
    }

    @Override
    public List<Book> selectAllBooksByCondition(Book book) {
        return bookDao.selectByCondition(book);
    }

    @Override
    public List<Book> searchAllByTypeBooks(int cid) {
        return bookDao.searchAllBooksByType(cid);
    }

    @Override
    public boolean modify(Book book) {
        return bookDao.updateById(book) > 0;
    }
}
